package io.wongxd.demo.page.views

import androidx.compose.foundation.*
import androidx.compose.foundation.gestures.animateScrollBy
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import io.wongxd.demo.resourcePainter
import io.wongxd.solution.baseView.BaseComposeToolBarFgt
import io.wongxd.solution.compose.custom.views.chain_scrollable_component.ChainMode
import io.wongxd.solution.compose.custom.views.chain_scrollable_component.ChainScrollableComponent
import io.wongxd.solution.compose.custom.views.chain_scrollable_component.ChainScrollableComponentState
import io.wongxd.solution.compose.custom.views.chain_scrollable_component.scrollable_appbar.ScrollableAppBar
import io.wongxd.solution.compose.custom.views.other.FpsText
import io.wongxd.solution.compose.custom.views.util.ComposePosition
import kotlinx.coroutines.launch
import kotlin.math.roundToInt

class FgtViewScrollableAppBar : BaseComposeToolBarFgt() {
    override fun lazyInit() {

    }

    override val title: String
        get() = "FgtViewScrollableAppBar"

    @Composable
    override fun Render() {
        ComposeContent()
    }

    private var composePosition by mutableStateOf(ComposePosition.Top)
    private var chainMode by mutableStateOf(ChainMode.ChainContentFirst)
    private val maxDp = 200.dp
    private val minDp = 56.dp

    @Composable
    private fun ComposeContent() {
        Column(modifier = Modifier.fillMaxSize()) {
            AppBar()
            Menu()
            ChainScrollable()
        }
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    private fun Menu() {
        Row {
            Text(text = "方向:$composePosition")
            Button(onClick = {
                composePosition = when (composePosition) {
                    ComposePosition.Top -> ComposePosition.Bottom
                    ComposePosition.Bottom -> ComposePosition.Start
                    ComposePosition.Start -> ComposePosition.End
                    ComposePosition.End -> ComposePosition.Top
                }
            }) {
                Text(text = "切方向")
            }
            FpsText(modifier = Modifier)
            Text(text = "模式:${chainMode.toString().substring(0, 5)}")
            Button(onClick = {
                chainMode =
                    if (chainMode == ChainMode.ChainContentFirst) ChainMode.ContentFirst else ChainMode.ChainContentFirst
            }) {
                Text(text = "切模式")
            }
        }
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun ColumnScope.ChainScrollable() {
        ChainScrollableComponent(
            minScrollPosition = minDp,
            maxScrollPosition = maxDp,
            chainContent = { state ->
                Box(modifier = Modifier
                    .let {
                        if (composePosition.isHorizontal()) it
                            .fillMaxHeight()
                            .width(maxDp)
                        else it
                            .fillMaxWidth()
                            .height(maxDp)
                    }
                    .offset {
                        when (composePosition) {
                            ComposePosition.Start -> IntOffset(
                                state
                                    .getScrollPositionValue()
                                    .roundToInt(), 0
                            )
                            ComposePosition.End -> IntOffset(
                                state
                                    .getScrollPositionValue()
                                    .roundToInt(), 0
                            )
                            ComposePosition.Top -> IntOffset(
                                0,
                                state
                                    .getScrollPositionValue()
                                    .roundToInt()
                            )
                            ComposePosition.Bottom -> IntOffset(
                                0,
                                state
                                    .getScrollPositionValue()
                                    .roundToInt()
                            )
                        }
                    }
                    .background(Color.LightGray)) {
                    Text(
                        text = "${state.getScrollPositionValue()}  ${state.getScrollPositionPercentage()}",
                        modifier = Modifier.align(
                            when (composePosition) {
                                ComposePosition.Start -> Alignment.CenterEnd
                                ComposePosition.End -> Alignment.CenterStart
                                ComposePosition.Top -> Alignment.BottomCenter
                                ComposePosition.Bottom -> Alignment.TopCenter
                            }
                        )
                    )
                }
            },
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f),
            chainMode = chainMode,
            composePosition = composePosition,
        ) {
            if (false) {
                LazyColumn(
                    contentPadding = PaddingValues(top = maxDp), modifier = Modifier.fillMaxSize()
                ) {
                    items(100) {
                        Text("item $it")
                    }
                }
            } else {
                if (composePosition.isHorizontal()) Row(
                    Modifier
                        .fillMaxSize()
                        .horizontalScroll(rememberScrollState())
                        .padding(
                            start = if (composePosition == ComposePosition.Start) maxDp else 0.dp,
                            top = if (composePosition == ComposePosition.Top) maxDp else 0.dp,
                            end = if (composePosition == ComposePosition.End) maxDp else 0.dp,
                            bottom = if (composePosition == ComposePosition.Bottom) maxDp else 0.dp,
                        )
                ) {
                    repeat(100) {
                        Text("item $it")
                    }
                }
                else Column(
                    Modifier
                        .fillMaxSize()
                        .verticalScroll(rememberScrollState())
                        .padding(
                            start = if (composePosition == ComposePosition.Start) maxDp else 0.dp,
                            top = if (composePosition == ComposePosition.Top) maxDp else 0.dp,
                            end = if (composePosition == ComposePosition.End) maxDp else 0.dp,
                            bottom = if (composePosition == ComposePosition.Bottom) maxDp else 0.dp,
                        )
                ) {
                    repeat(100) {
                        Text("item $it")
                    }
                }
            }
        }
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun ColumnScope.AppBar() {
        val lazyListState = rememberLazyListState()
        ScrollableAppBar(
            title = "toolbar",
            background = resourcePainter("top_bar_bk"),
            maxScrollPosition = maxDp,
            modifier = Modifier
                .weight(1f)
                .fillMaxWidth(),
            //onScrollStop = scrollStop(lazyListState)
        ) {
            LazyColumn(
                contentPadding = PaddingValues(top = maxDp),
                modifier = Modifier.fillMaxSize(),
                state = lazyListState,
            ) {
                items(100) { index ->
                    Text(
                        "I'm item $index", modifier = Modifier.padding(16.dp)
                    )
                }
            }
        }
    }

    //停止拖动时,使appbar归位
    private fun scrollStop(lazyListState: LazyListState): (ChainScrollableComponentState) -> Unit =
        function@{ state ->
            val percentage = state.getScrollPositionPercentage()
            if (percentage == 1f || percentage == 0f) return@function
            state.coroutineScope.launch {
                val startPositionValue = state.getScrollPositionValue()
                if (percentage > 0.5f) {
                    state.setScrollPositionWithAnimate(state.minPx - state.maxPx)
                    lazyListState.animateScrollBy(startPositionValue - state.minPx)
                } else {
                    state.setScrollPositionWithAnimate(0f)
                    lazyListState.animateScrollBy(startPositionValue)
                }
            }
        }

    @Preview(showBackground = true, backgroundColor = 0xffffffff)
    @Composable
    fun RenderPreView() {

    }
}